package m2.glre.etalions;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ChallengeActivity extends Activity {
	private ChocListener mShaker;
	MediaPlayer mp;

	public MediaPlayer getMp() {
		return mp;
	}

	public void setMp(MediaPlayer mp) {
		this.mp = mp;
	}

	// Le Menu
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.choix1:
			mp.setVolume(0.3f, 0.3f);
			return true;
		case R.id.quitter:
			System.exit(-1);
			return true;
		}
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mp = MediaPlayer.create(getBaseContext(), R.raw.praan);
		mp.setVolume(0.1f, 0.1f);

		final Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
		setMp(mp);

		mShaker = new ChocListener(this);
		mShaker.setMp(mp);
		 mShaker.setOnShakeListener(new ChocListener.OnShakeListener() {
		 public void onShake() {
		
		 vibe.vibrate(100);
		
		 }
		
		
		 });

		// start
		OnClickListener ButtonConnexion1 = new OnClickListener() {
			public void onClick(View actuelView) {
				if (!mp.isPlaying())
					mp.start();

			}
		};

		ImageButton imgbouton1 = (ImageButton) findViewById(R.id.imageButton1);
		imgbouton1.setOnClickListener(ButtonConnexion1);

		// pause
		OnClickListener ButtonConnexion2 = new OnClickListener() {
			public void onClick(View actuelView) {
				if (mp.isPlaying())
					mp.pause();
			}
		};

		ImageButton imgbouton2 = (ImageButton) findViewById(R.id.imageButton2);
		imgbouton2.setOnClickListener(ButtonConnexion2);

		// stop
		OnClickListener ButtonConnexion3 = new OnClickListener() {
			public void onClick(View actuelView) {
				// m.stop();

			}
		};

		ImageButton imgbouton3 = (ImageButton) findViewById(R.id.imageButton3);
		imgbouton3.setOnClickListener(ButtonConnexion3);

	}

	@Override
	public void onResume() {
		mShaker.resume();
		super.onResume();
	}

	@Override
	public void onPause() {
		mShaker.pause();
		mp.pause();
		super.onPause();
	}

}